package com.css.fxfzcpxx.modules.typeManage.controller;

import com.css.fxfzcpxx.base.response.RestResponse;
import com.css.fxfzcpxx.modules.typeManage.entity.TypeManage;
import com.css.fxfzcpxx.modules.typeManage.param.Transfer;
import com.css.fxfzcpxx.modules.typeManage.param.TransferByTypeIdParam;
import com.css.fxfzcpxx.modules.typeManage.param.TypeManageParam;
import com.css.fxfzcpxx.modules.typeManage.service.TypeManageService;
import com.css.fxfzcpxx.modules.websocket.WebSocketManager;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/03/08
 * @Description: 类别管理
 */
@Api(tags ="类别管理")
@Slf4j
@RestController
@RequestMapping("/typeManage")
public class TypeManageController {

    @Resource
    private TypeManageService typeManageService;

    private final WebSocketManager webSocketManager;

    @Autowired
    public TypeManageController(WebSocketManager webSocketManager) {
        this.webSocketManager = webSocketManager;
    }

    /**
     * 获取类别下拉根据板块名称
     * @param plate
     * @return
     */
    @GetMapping("/getType")
    public RestResponse getType(String plate) {
        RestResponse restResponse = null;
        try {
            List<TypeManage> list = typeManageService.getType(plate);
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 保存
     *
     * @param typeManage
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody TypeManage typeManage) {
        RestResponse response = null;
        try {
            response = typeManageService.save(typeManage);
            webSocketManager.broadcastMessage("类别管理新增啦","typeManage");
        } catch (Exception e) {
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 删除
     * @param
     * @return
     */
    @PostMapping("/delete/{id}/{typePlate}")
    public RestResponse delete(@PathVariable("id") String id,@PathVariable("typePlate") String typePlate){
        RestResponse response = null;
        try{
            response = typeManageService.delete(id,typePlate);
            webSocketManager.broadcastMessage("类别管理删除啦","typeManage");
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 查询数据
     * @param param
     * @return
     */
    @GetMapping("/getListByParam")
    public RestResponse getListByParam(TypeManageParam param) {
        RestResponse restResponse = null;
        try {
            restResponse = typeManageService.getListByParam(param);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 根据数据迁移
     //* @param typePlate 板块
     //* @param ids 要转移的数据
     //* @param typeId 转移到这个类别id下
     * @return
     */
    @PostMapping("/transferByIds")
    public RestResponse transferByIds( @RequestBody Transfer transfer){
        RestResponse response = null;
        try{
            typeManageService.transferByIds(transfer.getTypePlate(),transfer.getTypeId(),transfer.getIds());
            response = RestResponse.succeed("迁移成功");
        }catch (Exception e){
            String errorMessage = "迁移失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 根据类别迁移
     //* @param typePlate 板块
     //* @param oldTypeIds 老的类别ids用,分隔
     //* @param newTypeId 转移到这个类别id下
     * @return
     */
    @PostMapping("/transferByTypeId")
    public RestResponse transferByTypeId(@RequestBody TransferByTypeIdParam transfer){
        RestResponse response = null;
        try{
            typeManageService.transferByTypeId(transfer.getTypePlate(),transfer.getOldTypeIds(),transfer.getNewTypeId());
            response = RestResponse.succeed("迁移成功");
        }catch (Exception e){
            String errorMessage = "迁移失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 修改
     * @param
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody TypeManage typeManage){
        RestResponse response = null;
        try{
            typeManageService.update(typeManage);
            webSocketManager.broadcastMessage("类别管理修改啦","typeManage");
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 根据id查询
     * @param id 主键id
     * @return
     */
    @GetMapping("/findById")
    @Operation(summary = "根据id查询")
    public RestResponse findById(String id) {
        try {
            TypeManage data = typeManageService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }
}
